package com.infomanage.service.worklog;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.bean.TimeBean;
import com.infomanage.dao.AttendanceDAO;
import com.infomanage.dao.PersonDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.Attendance;
import com.infomanage.po.Person;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.util.TimeUtil;
import com.infomanage.vo.AttendanceItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

public class AttendanceService {

	public static AttendanceItem loadAttendance(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			String attendanceId = request.getParameter("attendanceId");
			if (StringUtils.isEmpty(attendanceId))
				return null;
			AttendanceDAO attendanceDAO = new AttendanceDAO();
			return attendanceDAO.find(Integer.parseInt(attendanceId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<AttendanceItem> loadAttendanceList(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			AttendanceDAO attendanceDAO = new AttendanceDAO();
			List<AttendanceItem> list = attendanceDAO.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ReturnObject saveAttendanceInfo(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			Attendance att = new Attendance();
			att.setPersonId(Integer.parseInt(request.getParameter("personId")));
			att.setBegintime(request.getParameter("begintime"));
			att.setEndtime(request.getParameter("endtime"));
			att.setAttendanceTime(TimeUtil.parseToYearMonth(request.getParameter("begintime")));
			att.setResult(Integer.parseInt(request.getParameter("result")));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("personId")));
			if(person != null){
				att.setDeptId(person.getDeptId());
			}
			att.setDescription(request.getParameter("description") == null ? "" : request.getParameter("description"));
			AttendanceDAO attendanceDAO = new AttendanceDAO();
			attendanceDAO.add(att);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject modifyAttendanceInfo(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int planId = Integer.parseInt(request.getParameter("attendanceId"));
			AttendanceDAO dao = new AttendanceDAO();
			Attendance att = dao.find(Attendance.class, planId);
			att.setPersonId(Integer.parseInt(request.getParameter("personId")));
			att.setBegintime(request.getParameter("begintime"));
			att.setEndtime(request.getParameter("endtime"));
			att.setAttendanceTime(TimeUtil.parseToYearMonth(request.getParameter("begintime")));
			att.setResult(Integer.parseInt(request.getParameter("result")));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("personId")));
			if(person != null){
				att.setDeptId(person.getDeptId());
			}
			att.setDescription(request.getParameter("description") == null ? "" : request.getParameter("description"));
			dao.modify(att);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}

	public static ReturnObject removeAttendanceInfo(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String attendanceId = request.getParameter("attendanceId");
			AttendanceDAO attendanceDAO = new AttendanceDAO();
			attendanceDAO.remove(Attendance.class, Integer.parseInt(attendanceId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static List<AttendanceItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			AttendanceDAO dao = new AttendanceDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			// 设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if (currPageNum == null) {
				page.setCurrentPageNum(1);
			} else {
				page.setCurrentPageNum(Integer.parseInt(currPageNum));

			}
			searchBean.setPage(page);
			List<AttendanceItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String deptId = request.getParameter("deptId");
		String personId = request.getParameter("personId");
		String beginDay = request.getParameter("beginDay");
		String endDay = request.getParameter("endDay");
		SearchBean searchBean = new SearchBean();
		if (!StringUtil.isEmpty(deptId)) {
			FieldBean field = new FieldBean("deptId", Integer.parseInt(deptId));
			searchBean.add(field);
		}
		if (!StringUtil.isEmpty(personId)) {
			FieldBean field = new FieldBean("personId", Integer.parseInt(personId));
			searchBean.add(field);
		}
		if (!StringUtil.isEmpty(beginDay)) {
			searchBean.setBeginTimeName("begintime");
			searchBean.setBeginTimeValue(beginDay);
		}
		if (!StringUtil.isEmpty(endDay)) {
			searchBean.setEndTimeName("endtime");
			searchBean.setEndTimeValue(endDay);
		}
		
		// 设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if (user != null) {
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			AttendanceDAO dao = new AttendanceDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)/PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	public static ReturnObject removeBatchAttendance(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			AttendanceDAO dao = new AttendanceDAO();
			if(batchIds != null){
				String[] ids = batchIds.split(";");
				for(String id : ids){
					dao.remove(Attendance.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
}
